package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.ChatRoom;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.ChatRoomMapper;
import cn.wolfcode.trip.base.mapper.ChatRoomStateMapper;
import cn.wolfcode.trip.base.service.IChatRoomService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聊天室资源控制器
 */
@RestController
@RequestMapping("/chatRooms")
public class ChatRoomController {

    @Autowired
    private IChatRoomService roomService;

    @Autowired
    private ChatRoomMapper chatRoomMapper;


    @PostMapping("/create")
    public JSONResult createRoom(String[] str,String roomName){
        roomService.createRoom(str,roomName);
        System.out.println(123);
        return new JSONResult();
    }

    /**
     * 根据当前登录用户信息查询自己所在聊天室的信息
     */
    @GetMapping("{loginId}")
    public List<ChatRoom> selectRoomUsersByLoginId(@PathVariable Long loginId){
        List<ChatRoom> list = roomService.selectRoomUsersByLoginId(loginId);
        return list;
    }

    /**
     * 根据聊天室id查询当前所在聊天室的所有成员的信息
     */
    @GetMapping("/users/{roomId}")
    public List<User> selectRoomUsersByRoomId(@PathVariable Long roomId){
        List<User> list = roomService.selectRoomUsersByRoomId(roomId);
        return list;
    }


    /**
     * 查询聊天室名字和当前聊天室所有内容
     * @param roomId
     * @return
     */
    @GetMapping("roomName")
    public List<ChatRoom> selectRoomName(Long roomId){
        return roomService.selectRoomName(roomId);
    }

    /**
     * 根据保存聊天室内容
     * @return
     */
    @PostMapping
    public JSONResult save(ChatRoom chatRoom){
        System.out.println(123);
        roomService.insert(chatRoom);
        return new JSONResult();
    }

    /**
     * 根据聊天室id查询最新聊天记录
     * @param roomId
     * @return
     */
    @GetMapping
    public List<ChatRoom> get(Long roomId, Long userId){
        //查询当前用户的未读信息
        List<ChatRoom> list = roomService.selectNewMessage(roomId,userId);
        if (list != null) {
            for (ChatRoom room : list) {
                roomService.chanegSendState(room.getId(),userId);
            }
        }
        return list;
    }
}
